//
//  ConsultView.m
//  SHHCPhone
//
//  Created by sheng long on 14-6-29.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "ConsultView.h"

#define UITextViewMinHeight     35.1
#define UITextViewMaxHeight     73.2

#define BottomSendViewTag       8690

@interface ConsultView()<UITextViewDelegate,AutoRefreshTableViewDelegate>
{
    //输入框
    UITextView *_chatText;
}
@end

@implementation ConsultView

@synthesize mainTable=_mainTable;
@synthesize superVC;
@synthesize chatText=_chatText;

- (id)initWithFrame:(CGRect)frame superVC:(ConsultViewController*)vc
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.superVC = vc;
        
        _mainTable = [[ConsultTableView alloc] initWithFrame:self.bounds];
        [_mainTable setDelegate:self];
        [_mainTable setCurrentRefreshType:REFRESH_STATE];
        [_mainTable setSuperVC:vc];
        [self addSubview:_mainTable];
        [self showBottomView];
    }
    return self;
}

-(void)addObserver
{
    [[NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged) name:UITextViewTextDidChangeNotification object:nil];
}

-(void)removeObserver
{
    [[NSNotificationCenter defaultCenter ]removeObserver:self];
}

//显示底部发送消息框
-(void)showBottomView
{
    CGFloat bottomViewHeight = 44;
    
    CGRect rect = _mainTable.frame;
    rect.size.height-=bottomViewHeight;
    [_mainTable setFrame:rect];
    [_mainTable.mainTable setFrame:rect];
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-bottomViewHeight, self.frame.size.width, bottomViewHeight)];
    [bottomView setBackgroundColor:[UIColor whiteColor]];
    [self addSubview:bottomView];
    
    //间隔线
    UIView *gapView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, bottomView.frame.size.width, 1)];
    [gapView1 setBackgroundColor:[z_UI colorWithHexString:@"e5e3e3"]];
    [bottomView addSubview:gapView1];
    UIView *gapView2 = [[UIView alloc] initWithFrame:CGRectMake(gapView1.frame.origin.x, gapView1.frame.origin.y+1, gapView1.frame.size.width, 1)];
    [gapView2 setBackgroundColor:[UIColor whiteColor]];
    [bottomView addSubview:gapView2];

    _chatText = [[UITextView alloc] initWithFrame:CGRectMake(15, (bottomView.frame.size.height-UITextViewMinHeight)/2, bottomView.frame.size.width-72-15, UITextViewMinHeight)];
    [_chatText setDelegate:self];
    [_chatText setFont:[UIFont systemFontOfSize:16]];
    [_chatText setReturnKeyType:UIReturnKeyDone];
    [bottomView addSubview:_chatText];
    
    //发送View
    UIView *sendView = [[UIView alloc] initWithFrame:CGRectMake(bottomView.frame.size.width-63, 0, 63, bottomView.frame.size.height)];
    [bottomView addSubview:sendView];
    [sendView setTag:BottomSendViewTag];
    [sendView setBackgroundColor:[UIColor clearColor]];
    
    //间隔线
    UIView *gapView3 = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 1, sendView.frame.size.height-5*2)];
    [gapView3 setBackgroundColor:[z_UI colorWithHexString:@"e5e3e3"]];
    [sendView addSubview:gapView3];
    UIView *gapView4 = [[UIView alloc] initWithFrame:CGRectMake(gapView3.frame.origin.x+1, gapView3.frame.origin.y, 1, gapView3.frame.size.height)];
    [gapView4 setBackgroundColor:[UIColor whiteColor]];
    [sendView addSubview:gapView4];
    
    //
    UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [sendButton setFrame:CGRectMake(gapView4.frame.origin.x+1, 0, sendView.frame.size.width-gapView4.frame.origin.x-1, sendView.frame.size.height)];
    [sendButton setTitle:@"发送" forState:UIControlStateNormal];
    [sendButton setTitleColor:[z_UI colorWithHexString:@"4d4d4d"] forState:UIControlStateNormal];
    [sendButton setTitleColor:[z_UI colorWithHexString:@"d2d0d0"] forState:UIControlStateHighlighted];
    [sendButton.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
    [sendButton addTarget:self action:@selector(sendButt) forControlEvents:UIControlEventTouchUpInside];
    [sendView addSubview:sendButton];
}

-(void)sendButt
{
    //
    NSString *text = [_chatText.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (text.length == 0) {
        CLog(@"请输入消息内容");
        [LoadingFrameView autoDisappearTipview:@"请输入消息内容"];
    }else{
//        ConsultInfo *info = [[ConsultInfo alloc] init];
//        int randomNumber = arc4random();
//        if (randomNumber%2) {
//            info.myTitle = _chatText.text;
//        }else{
//            info.title = _chatText.text;
//        }
//        if (superVC.consultType == KF_TYPE) {
//            info.time = [z_DateTime encodeTime:[NSDate date]];
//        }
        ConsultInfo *info = [[ConsultInfo alloc] init];
        info.content = _chatText.text;
        [self.chatText resignFirstResponder];
        [self.superVC sendMsg:info];
//        [_mainTable addSendMsg:info];
//        //发送消息
//        _chatText.text = nil;
    }
}

#pragma mark - AutoRefreshTableViewDelegate
//刷新数据
-(void)refreshTableData
{
    [self.superVC loadingMoreTableData];
}

#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

//UITextViewTextDidChangeNotification
- (void)textChanged
{
    CGFloat oldHeight = _chatText.frame.size.height;
    CGFloat newHeight = [self heightForTextView:_chatText WithText:_chatText.text];
    CLog(@"Height====%@===%f",_chatText.text,newHeight);
    if (newHeight!=oldHeight) {
        
        //设置uitextview大小
        CGRect textRect = _chatText.frame;
        textRect.size.height = newHeight;
        [_chatText setFrame:textRect];
        
        //设置底部位置
        UIView *superView = _chatText.superview;
        CGRect superRect = superView.frame;
        superRect.size.height+=(newHeight-oldHeight);
        superRect.origin.y-=(newHeight-oldHeight);
        [superView setFrame:superRect];
        
        //设置底部发送按钮位置
        UIView *sendView = [superView viewWithTag:BottomSendViewTag];
        [sendView setCenter:CGPointMake(sendView.center.x, superRect.size.height/2)];
        
        //设置对话列表位置
        CGRect tableRect = _mainTable.frame;
        tableRect.size.height-=(newHeight-oldHeight);
        [_mainTable setFrame:tableRect];
        [_mainTable.mainTable setFrame:_mainTable.bounds];
    }
}

//获取uitextview的内容高度
- (float) heightForTextView:(UITextView *)textView WithText:(NSString *)strText
{
    float fPadding = 16.0; // 8.0px x 2
    CGSize constraint = CGSizeMake(textView.contentSize.width - fPadding, CGFLOAT_MAX);
    CGSize size = [z_UI returnHeightFloat:strText frontSize:textView.font frontWidth:constraint.width];
    
    float fHeight = size.height + fPadding;
    
    if (fHeight < UITextViewMinHeight) {
        return UITextViewMinHeight;
    }else if (fHeight > UITextViewMaxHeight){
        return UITextViewMaxHeight;
    }
    return fHeight;
}
#pragma mark - 键盘通知2
-(void)keyboardWillShow:(NSNotification *)aNotification
{
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    CGFloat height = keyboardRect.size.height;
    
    //底部位置改变
    UIView *superView = _chatText.superview;
    CGRect rect = superView.frame;
    rect.origin.y = self.frame.size.height-height-rect.size.height;
    [superView setFrame:rect];
    
    //会话显示table位置改变
    CGRect tableRect = _mainTable.frame;
    tableRect.size.height = rect.origin.y-tableRect.origin.y;
    [_mainTable setFrame:tableRect];
    [self keepMainTableLoc:_mainTable.bounds.size.height];
    [_mainTable.mainTable setFrame:_mainTable.bounds];
}

-(void)keyboardWillHide:(NSNotification *)aNotification
{
    //底部位置改变
    UIView *superView = _chatText.superview;
    CGRect rect = superView.frame;
    rect.origin.y = self.frame.size.height-rect.size.height;
    [superView setFrame:rect];
    
    //会话显示table位置改变
    CGRect tableRect = _mainTable.frame;
    tableRect.size.height = rect.origin.y-tableRect.origin.y;
    [_mainTable setFrame:tableRect];
    [self keepMainTableLoc:_mainTable.bounds.size.height];
    [_mainTable.mainTable setFrame:_mainTable.bounds];
}

//保持查看消息的位置
//height    改变mainTable大小之后的高度
-(void)keepMainTableLoc:(CGFloat)height
{
    //离底部多少距离
    CGFloat bottom = _mainTable.mainTable.contentSize.height-_mainTable.mainTable.contentOffset.y-_mainTable.mainTable.frame.size.height;
    CGFloat y = _mainTable.mainTable.contentSize.height - height - bottom;
    if (y>=0) {
        [_mainTable.mainTable setContentOffset:CGPointMake(0, y)];
    }
}

@end
